package com.example.springboot.controller;

import com.example.springboot.common.Result;
import com.example.springboot.common.ResultCode;
import com.example.springboot.domain.po.Grade;
import com.example.springboot.domain.po.Grade;
import com.example.springboot.exception.CustomException;
import com.example.springboot.service.GradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：伐柯梦姝
 * @Package：com.example.springboot.controller
 * @Project：springboot
 * @name：GradeController
 * @Date：2025/2/7 20:36
 * @Filename：GradeController
 * @for:
 */
@Api(tags = "班级管理接口")
@RestController
@CrossOrigin
@RequestMapping("/grade")
public class GradeController {
       @Resource
       private GradeService gradeService;

       @ApiOperation("查询管理员接口")
       @GetMapping("/alldata")
       public Result getData(){
              List<Grade> gradeList = gradeService.getAll();
              if (gradeList.size() < 1){
                     throw new CustomException(ResultCode.DATA_ADMIN_LESS);
              }
              return Result.success(gradeList);

       }
       @ApiOperation("新增班级接口")
       @PostMapping
       public Result addGrade(@RequestBody Grade grade){

              return Result.success(gradeService.gradeRegister(grade));
       }

       @ApiOperation("删除班级接口")
       @DeleteMapping("/{id}")
       public Result delete(@PathVariable Integer id){
              gradeService.deleteById(id);
              return Result.success();
       }

       @ApiOperation("修改班级接口")
       @PutMapping
       public Result update(@RequestBody Grade grade){
              gradeService.update(grade);
              return Result.success();
       }

       @ApiOperation("分页查询班级接口")
       @PostMapping("/page")
       public Result page(@RequestBody Grade search,
                          @RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "5") Integer pageSize){
              return Result.success(gradeService.findPage(search, pageNum, pageSize));
       }
}
